package com.wj.taobaoke.naifen;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import com.wj.taobaoke.AnalysisWeb;
import com.wj.taobaoke.BlogItem;
import com.wj.taobaoke.GenerateBlogFile;
import com.wj.taobaoke.GuanJianZi;
import com.wj.taobaoke.ReadTemplate;

public class Analysist_qbaobei {
	public static List<BlogItem> blogItems;
	
	public static List<BlogItem> errorItems = new ArrayList<BlogItem>();
	
	public static void main(String[] args) {
		
		String templateFileName = "naifenTemplate.html";
		ReadTemplate readTemplate = new ReadTemplate();
		readTemplate.readTemplateList(templateFileName);

		GenerateBlogFile generateBlogFile = new GenerateBlogFile(readTemplate);
		
		AnalysisWeb analysisWeb = new AnalysisWeb();
		for(GuanJianZi guanJianZi : NaiFenGuanJianZi.getAll()){
			guanJianZi.init(readTemplate.getProjectPath());
			File pathFile = new File(guanJianZi.getBlogFilePath());
			if(!pathFile.exists()){
				pathFile.mkdir();
			}
//			String html = analysisWeb.getOneHtml(guanJianZi.baiduNewsUrl, "gb2312", false);
//			html = analysisWeb.getForRegex(html, "<div class=\"main\">(.*?)<div class=\"jk_yezs_top\">");
//			List<String> hrefList = analysisWeb.getContentListFroRegex(html, "<a(.*?)</a>");
//			Set<String> urlList = new HashSet<String>();
//			for (String string : hrefList) {
//				String newsUrl = analysisWeb.getForRegex(string, "www.qbaobei(.*?).html");
//				newsUrl = newsUrl.replaceAll("_\\d.html", ".html");
//				if(newsUrl != null && newsUrl.length() > 0){
//					urlList.add("http://"+newsUrl);
//				}
//			}
			String html = analysisWeb.getOneHtml(guanJianZi.baiduNewsUrl, "utf-8", false);
			List<String> hrefList = analysisWeb.getContentListFroRegex(html, "<cite(.*?)</cite>");
			Set<String> urlList = new HashSet<String>();
			for (String string : hrefList) {
				String newsUrl = analysisWeb.getForRegex(string, "www.qbaobei(.*?).html");
				if(newsUrl != null && newsUrl.length() > 0){
					urlList.add("http://"+newsUrl);
				}
			}
			if(guanJianZi.baiduNewsUrl.endsWith("page=1")){
				html = analysisWeb.getOneHtml(guanJianZi.baiduNewsUrl.replace("page=1", "page=2"), "utf-8", false);
				hrefList = analysisWeb.getContentListFroRegex(html, "<cite(.*?)</cite>");
				for (String string : hrefList) {
					String newsUrl = analysisWeb.getForRegex(string, "www.qbaobei(.*?).html");
					if(newsUrl != null && newsUrl.length() > 0){
						urlList.add("http://"+newsUrl);
					}
				}
			}
			blogItems = new ArrayList<BlogItem>();
			int counter = 1;
			for (String newsUrl : urlList) {
				BlogItem blogItem = new BlogItem();
				blogItem.index = counter;
				blogItem.contentUrl = newsUrl;
				new GenerateBlogItemHtmlFile(blogItem, guanJianZi, readTemplate).run();
				blogItems.add(blogItem);
				counter++;
			}
			while(errorItems.size() > 0){
				for (BlogItem blogItem : errorItems) {
					new GenerateBlogItemHtmlFile(blogItem, guanJianZi, readTemplate).run();
				}
			}
			generateBlogFile.createIndexHtmlFile(blogItems, guanJianZi);
			System.out.println(guanJianZi.guanJianZi+"完成");
		}
//		BlogItem blogItem = new BlogItem();
//		blogItem.contentUrl = "http://www.qbaobei.com/naifen/news/20120525_197733_4.html";
//		blogItem.contentUrl = blogItem.contentUrl.replaceAll("_\\d.html", ".html");
//		GenerateBlogItemHtmlFile blogItemHtmlFile = new GenerateBlogItemHtmlFile(blogItem, null, null);
//		blogItemHtmlFile.analysisNewsHtml(1);
	}
	
	
}


class GenerateBlogItemHtmlFile{

	private AnalysisWeb analysisWeb;
	private BlogItem blogItem;
	private GuanJianZi guanJianZi;
	private ReadTemplate readTemplate;
	
	
	public GenerateBlogItemHtmlFile(BlogItem blogItem, GuanJianZi guanJianZi, ReadTemplate readTemplate){
		this.blogItem = blogItem;
		this.guanJianZi = guanJianZi;
		this.readTemplate = readTemplate;
	}
	public void run() {
		List<String> contentList = analysisNewsHtml(1);//写之前先分析哦
		for(int page = 1; page <= blogItem.pageCount; page++){
			contentList.addAll(analysisNewsHtml(page));
		}
		generateBlogHtmlFile(contentList);
		Analysist_qbaobei.blogItems.remove(blogItem);
		System.out.println(guanJianZi.guanJianZi+"_"+blogItem.index+"_success, title="+blogItem.hiddenTitle);
	}

	private boolean generateBlogHtmlFile(List<String> contentList){
		FileWriter fileWriter = null;
		 try{
			 fileWriter = new FileWriter(guanJianZi.getBlogFilePath()+blogItem.index+".html");
			 for(String string : readTemplate.templateStrList){
				 if("<!--news flag-->".equals(string)){
					fileWriter.write(string);
					fileWriter.write("\r\n");
					writeNewsHtml(fileWriter, contentList);//在此处开始写新闻正文
				 }else if("<!--page navigation flag-->".equals(string)){
					 	for(String navStr : readTemplate.navStrList){
					 		if("<!--page navigation-->".equals(navStr)){
					 			fileWriter.write(guanJianZi.getGuanJianZiNav()+blogItem.hiddenTitle);
					 		}else{
					 			fileWriter.write(navStr);
					 		}
					 	}
			 			fileWriter.write("\r\n");
				}else{
					string = string.replaceFirst("<title>关键字", "<title>"+blogItem.hiddenTitle);
					string = string.replaceAll("<meta name=\"keywords\" content=\"关键字\"", "<meta name=\"keywords\" content=\""+blogItem.hiddenTitle+"\"");
					string = string.replaceAll("<meta name=\"description\" content=\"关键字\"", "<meta name=\"description\" content=\""+blogItem.hiddenTitle+"\"");

					fileWriter.write(string);
					fileWriter.write("\r\n");
				 }
			 }
		 }catch(Exception e){
			 e.printStackTrace();
		 }finally{
			 if(fileWriter != null){
				 try{
					 fileWriter.flush();
					 fileWriter.close();
				 }catch(Exception e){
					 e.printStackTrace();
				 }
			 }
		 }
		 return true;
	}
	//这个方法是写新闻正文
	private void writeNewsHtml(FileWriter fileWriter, List<String> contentList) throws IOException{
		 fileWriter.write("<div class=\"left \"><div class=\"brief bgFFFFFF\"><h1>"+blogItem.hiddenTitle+"</h1><div class=\"blogcontent\">");
		 fileWriter.write("<div class=\"info\">来源："+blogItem.createTime+"</div>");
		 fileWriter.write("<div class=\"intro\">"+blogItem.content+"</div>");
		 writeOneGoodsFrameHtml(fileWriter);
		 for (String string : contentList) {
			string = string.replaceAll("<!--(.*?)-->", "");
			string = string.replaceAll("href(.*?)\"(.*?)\"", "href=\""+guanJianZi.guanJianZiBlogUrl+"index.html\"");
			string = string.replaceAll("href(.*?)'(.*?)'", "href=\""+guanJianZi.guanJianZiBlogUrl+"index.html\"");
			if(string.matches("(.*?)<a(.*?)>2</a>(.*?)")){
				break;
			}
			fileWriter.write(string);
		 }
		 fileWriter.write("</div></div></div>");		
	}

	private void writeOneGoodsFrameHtml(FileWriter fileWriter) throws IOException{
		fileWriter.write("<p align=\"center\" style=\"text-indent: 3px;\">");
		fileWriter.write("<script type=\"text/javascript\" language=\"javascript\">document.writeln(\"<Iframe src=\\\""+
				guanJianZi.guanJianZiGoodsUrl+"1.html"+
				"\\\" width=\\\"460px\\\" height=\\\"460px\\\" scrolling=\\\"no\\\" frameborder=\\\"0\\\"></Iframe>\");</script>");
		fileWriter.write("</p>");
	}
	//这个是分析网页
	public List<String> analysisNewsHtml(int page){
		Analysist_qbaobei.errorItems.remove(blogItem);
		analysisWeb = new AnalysisWeb();
		String html = analysisWeb.getOneHtml(blogItem.contentUrl, "utf-8", true);
		List<String> contentList = new ArrayList<String>();
		if(html.length() == 0){
			System.out.println(blogItem.index+":not read html;"+blogItem.contentUrl);
			Analysist_qbaobei.errorItems.add(blogItem);
			return contentList;
		}
		if(page == 1){
			int pageCount = 1;
			for(int i = 2; i < 10; i++){
				String next = analysisWeb.getForRegex(html, "<a(.*?)>"+i+"</a>");
				if(next != null && next.length() > 0){
					pageCount = i;
				}else{
					break;
				}
			}
			blogItem.pageCount = pageCount;
			blogItem.title = clearTag(analysisWeb.getForRegex(html, "<h1>(.*?)</h1>").replaceAll("<.*?>", ""));
			blogItem.hiddenTitle = blogItem.title;
			blogItem.createTime = "亲亲宝贝&nbsp;&nbsp;&nbsp;"+analysisWeb.getContentListFroRegex(html, "<span class=\"cnt-a-time\">(.*?)</span>");
			blogItem.createTime = blogItem.createTime.replaceAll("<.*?>", "");
			blogItem.content = analysisWeb.getForRegex(html, "<b>导读：</b>(.*?)</div>");
			blogItem.content = clearTag(blogItem.content);
			blogItem.content = blogItem.content.replaceAll("(?i)<div(.*?)</div>", "");
			blogItem.content = blogItem.content.replaceAll("</br>", "");
			blogItem.content = blogItem.content.replaceAll("(?i)<span(.*?)>", "<span>");
		}

//		System.out.println(blogItem.title);
//		System.out.println(blogItem.createTime);
//		System.out.println(blogItem.content);
		List<String> tempList = analysisWeb.getContentListFroRegex(html, "<p>(.*?)</p>");
		if(tempList.size() > 0){
			for (String string : tempList) {
				String onetemp = clearTag(string);
				if(onetemp.replaceAll("<.*?>", "").length() == 0){
					continue;
				}
				contentList.add("<p>"+onetemp+"</p>");
//				System.out.println(onetemp);
			}
		}else{
			contentList = analysisWeb.getContentListFroRegex(html, "<table(.*?)</table>");
			if(contentList.size() == 0){
				contentList = analysisWeb.getContentListFroRegex(html, "<span class=\"cnt-a-content\">(.*?)</span>");
			}
			if(contentList.size() == 0){
				System.out.println(blogItem.index+": not find <div> or <p> or <td>;"+blogItem.contentUrl);
				Analysist_qbaobei.errorItems.add(blogItem);
			}
		}
		return contentList;
	}
	
	private String clearTag(String string){
		String onetemp = string.replaceAll("(?i)<p>", "");
		onetemp = onetemp.replaceAll("(?i)<input(.*?)>", "");
		onetemp = onetemp.replaceAll("(?i)</p>", "");
		onetemp = onetemp.replaceAll("(?i)<a(.*?)>", "");
		onetemp = onetemp.replaceAll("(?i)</a>", "");
		onetemp = onetemp.replaceAll("<!--(.*?)-->", "");
		onetemp = onetemp.replaceAll("(.*?)网小编温馨提示(.*?)", "");
		onetemp = onetemp.replaceAll(" ", "");
		onetemp = onetemp.replaceAll("	", "");
		onetemp = onetemp.replaceAll(" ", "");
		onetemp = onetemp.replaceAll(" ", "");
		onetemp = onetemp.replaceAll("(?i)<\\s*script(.*?)<\\s*/script\\s*>", "");
		return onetemp;
	}
}